/*
 * JerboArticles.java
 *
 * Created on March 7, 2008, 11:23 AM
 */

package jerbo;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.TableModel;
import jerbo.util.ResultSetTableModel;
import jerbo.util.TableSorter;
import org.jdesktop.application.Application;

/**
 *
 * @author  luigi
 */
public class JerboSuppliers extends javax.swing.JInternalFrame {

    java.util.ResourceBundle bundle = 
            java.util.ResourceBundle.getBundle("jerbo/resources/JerboSuppliers");
    
    
    /** Creates new form JerboArticles */
    public JerboSuppliers() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        topPanel = new javax.swing.JPanel();
        suppliersScrollPane = new javax.swing.JScrollPane();
        suppliersTable = new javax.swing.JTable();
        bottomPanel = new javax.swing.JPanel();
        closeButton = new javax.swing.JButton();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("jerbo/resources/JerboSuppliers"); // NOI18N
        setTitle(bundle.getString("title")); // NOI18N
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/jerbo/resources/leaf.png"))); // NOI18N
        setName("suppliersManager"); // NOI18N

        topPanel.setName("topPanel"); // NOI18N

        org.jdesktop.layout.GroupLayout topPanelLayout = new org.jdesktop.layout.GroupLayout(topPanel);
        topPanel.setLayout(topPanelLayout);
        topPanelLayout.setHorizontalGroup(
            topPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 732, Short.MAX_VALUE)
        );
        topPanelLayout.setVerticalGroup(
            topPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 133, Short.MAX_VALUE)
        );

        suppliersScrollPane.setName("suppliersScrollPane"); // NOI18N

        suppliersTable.setModel(getTableModel());
        suppliersTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        suppliersTable.setName("suppliersTable"); // NOI18N
        suppliersScrollPane.setViewportView(suppliersTable);

        bottomPanel.setName("bottomPanel"); // NOI18N

        closeButton.setText(bundle.getString("closeButton.text")); // NOI18N
        closeButton.setName("closeButton"); // NOI18N
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout bottomPanelLayout = new org.jdesktop.layout.GroupLayout(bottomPanel);
        bottomPanel.setLayout(bottomPanelLayout);
        bottomPanelLayout.setHorizontalGroup(
            bottomPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, bottomPanelLayout.createSequentialGroup()
                .addContainerGap(651, Short.MAX_VALUE)
                .add(closeButton)
                .addContainerGap())
        );
        bottomPanelLayout.setVerticalGroup(
            bottomPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(bottomPanelLayout.createSequentialGroup()
                .add(closeButton)
                .addContainerGap(12, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(topPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(bottomPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(suppliersScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 708, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(topPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(suppliersScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 387, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(bottomPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_closeButtonActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel bottomPanel;
    private javax.swing.JButton closeButton;
    private javax.swing.JScrollPane suppliersScrollPane;
    private javax.swing.JTable suppliersTable;
    private javax.swing.JPanel topPanel;
    // End of variables declaration//GEN-END:variables
    

    protected TableModel getTableModel() {
        try {
            Connection conn = Application.getInstance(jerbo.JerboApp.class).getConnection();
            Statement stat = conn.createStatement();
            ResultSet result = stat.executeQuery("select * from suppliers;");
            TableSorter sorter = new TableSorter((TableModel)
                    new ResultSetTableModel(Application.getInstance(jerbo.JerboApp.class)
                    .cacheResultSet(result), bundle));
            sorter.setTableHeader(suppliersTable.getTableHeader());
            return sorter;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(JerboSuppliers.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(JerboSuppliers.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

}
